import random
import time
import ddt
from selenium import webdriver
from selenium.webdriver.support.ui import Select
from Base.BaseSetings import TestData_dir
# from WoniuBossPOM.Student_Management_POM_PZY import Stu_Sys_Mamagement
from WoniuBossPOM.All_Management_POM import All_Mamagement
from Base.PublicFunction import readTable

class WoniuBoss():
    def __init__(self):
        self.dr = All_Mamagement()

    # def login(self):
    #     self.dr.get_home_user_name_obj().send_keys('WNCD000')
    #     self.dr.get_home_password_obj().send_keys('woniu123')
    #     self.dr.get_home_login_obj().click()

    def test_stu(self,stuName):   #学员信息
        time.sleep(5)
        self.dr.get_home_student_obj().click()
        self.dr.get_home_stu_info_obj().click()
        #下拉框
        time.sleep(2)
        ele_select1 = self.dr.get_home_area_obj()   #【区域】下拉框
        Select(ele_select1).select_by_index(1)
        time.sleep(2)
        ele_select2 = self.dr.get_home_dire_obj()  #[方向]下拉框
        Select(ele_select2).select_by_index(3)
        time.sleep(2)
        ele_select3 = self.dr.get_home_class_obj()  # [班级]下拉框
        Select(ele_select3).select_by_index(1)
        time.sleep(2)
        ele_select4 = self.dr.get_home_status_obj() # [状态]下拉框
        Select(ele_select4).select_by_index(2)
        time.sleep(2)
        self.dr.get_home_stu_name_obj().send_keys(stuName)  #输入学员姓名
        time.sleep(2)
        self.dr.get_home_search_obj().click()  #点击【搜索】按钮


    def alter(self):  #修改操作
        self.dr.get_home_decode_obj().click()  #解密
        self.dr.get_home_decode1_obj().send_keys('woniu123')
        self.dr.get_home_access_obj().click()
        time.sleep(2)
        self.dr.get_home_alter_obj().click()  #[修改]按钮
        time.sleep(2)
        ele_select5 = self.dr.get_home_edu_obj()  # [学历]下拉框
        Select(ele_select5).select_by_index(1)
        time.sleep(2)
        self.dr.get_home_save_obj().click()  #[保存]按钮
        time.sleep(2)
        self.dr.get_home_access1_obj().click()   #[确认]按钮

    def search_user(self):   #用户管理  用户查询
        self.dr.get_home_system_obj().click()
        self.dr.get_home_system_user_obj().click()
        self.dr.get_home_staff_name_obj().send_keys('刘菲菲')
        self.dr.get_home_user_name_obj().send_keys('WNCD002')
        self.dr.get_home_search1_obj().click()
        result1 = self.dr.get_user().text
        return result1


    def alterrole(self):   #修改用户描述
        self.dr.get_home_system_obj().click()
        self.dr.get_home_system_user_obj().click()
        self.dr.get_home_user_name_obj().send_keys('WNCD002')
        time.sleep(2)
        self.dr.get_home_alterrole_obj().click()
        self.dr.get_home_alterrole1_obj().clear()
        self.dr.get_home_alterrole1_obj().send_keys('我是班主任')
        self.dr.get_home_saveuser_obj().click()
        time.sleep(2)
        self.dr.get_home_access5_obj().click()
        result2 = self.dr.get_teacherdata().text
        return result2



    def human_mangement_creatstaff(self,kwargs):
        time.sleep(6)
        self.dr.get_humanmange().click() #点击‘人事管理’
        self.dr.get_staffinfo().click() #点击‘员工信息’
        self.dr.get_creatbutton()[1].click()  # 点击新增
        time.sleep(3)
        self.dr.get_creatinfo(kwargs.get('info'))  #新增员工-区域
        time.sleep(3)
        self.dr.get_creatempment(kwargs.get('empment')) #部门
        self.dr.get_position()[1].send_keys(kwargs.get('positon')) #职位
        time.sleep(2)
        self.dr.get_name()[1].send_keys(kwargs.get('name')) #输入姓名
        time.sleep(2)
        self.dr.get_workid()[1].send_keys(kwargs.get('workid')) #输入工号
        self.dr.get_saving().click() #点击保存
        time.sleep(3)
        self.dr.get_determine().click() #点击确定
        time.sleep(3)
        self.dr.get_staffname().send_keys(kwargs.get('name'))
        self.dr.get_clicks().click()
        time.sleep(2)
        tableobj = self.dr.get_table()
        result= readTable(tableobj)

        return result

    def human_mangement_querystaff(self,region,dep,status,name):
        time.sleep(5)
        self.dr.get_humanmange().click() #点击‘人事管理’
        self.dr.get_staffinfo().click() #点击‘员工信息’
        self.dr.get_region(region)
        self.dr.get_dep(dep)
        self.dr.get_status(status)
        self.dr.get_staffname().click()
        self.dr.get_staffname().send_keys(name)
        self.dr.get_clicks().click()
        time.sleep(2)
        tableobj = self.dr.get_table()
        return readTable(tableobj)

    def cls_manage(self,cls_num,direction,teacher):  # 班级管理-学员转班
        self.dr.cls_management().click()            #点击班务管理
        self.dr.cls_manage().click()                #点击班级管理
        time.sleep(2)
        self.dr.add_button().click()                 #点击新增

        self.dr.cls_num().clear()                    #清空班号

        self.dr.cls_num().send_keys(cls_num)         #输入班号
        list=Select(self.dr.direction())             #方向下拉框修改,使其变成可输入文本
        list.select_by_visible_text(direction)
        self.dr.start_cls().click()                  #点击开班时间
        self.dr.start_cls_time().click()             #选择开班时间
        list=Select(self.dr.teacher())               #班主任下拉框修改
        list.select_by_visible_text(teacher)
        self.dr.save_button().click()               #点击保存 (42次操作)

        # self.obj.add_success().click()               #点击确定
        time.sleep(2)
        return self.dr.add_success().text           #返回新增班级成功文本

        time.sleep(2)
        # return self.obj.add_success().text           #返回新增班级成功文本
        self.dr.add_success().click()               #点击确定
        time.sleep(1)
        return self.dr.add().text           #返回新增


    # def readtable(self,tableObj):  # 自定义读表函数
    #     tbodyObj=tableObj.find_element_by_tag_name('tbody')
    #     trObjs=tbodyObj.find_elements_by_tag_name('tr')       #得到所有行
    #     full_list=[]
    #     for trObj in trObjs:                                  #得到每一行
    #         tdObjs=trObj.find_elements_by_tag_name('td')      #从每一行中得到所有列
    #         list=[]
    #         for tdObj in tdObjs:                              #遍历所有行里的每一列
    #             list.append(tdObj.text)                       #获取单元格文本，加第二级列表内
    #         full_list.append(list)                            #在把第二级列表加入大列表内
    #     return full_list                                      #返回二级列表


    def stu_leave(self,leave_day,stu_name,leave_cause):     #学员请假
        self.dr.cls_management().click()            #点击班务管理
        self.dr.stu_leave().click()                 #点击学员请假
        time.sleep(2)
        self.dr.button_add1().click()                #新增按钮
        self.dr.start_time().click()                 #开始时间
        self.dr.select_start_time().click()
        self.dr.end_time().click()                   #结束时间
        self.dr.select_end_time().click()
        time.sleep(2)
        self.dr.keepout_view().click()
        list=Select(self.dr.type_leave())            #选择请假类型
        count=len(list.options)
        index=random.randint(0,count-1)
        list.select_by_index(index)
        self.dr.leave_day().send_keys(leave_day)     #输入请假天数
        time.sleep(2)
        self.dr.stu_name().send_keys(stu_name)       #输入学生姓名
        time.sleep(1)
        self.dr.stu_name_button().click()            ###点击输入姓名后带出来的学号
        # self.obj.switch_to.alert.accept()
        # self.obj.stu_name().send_keys(Keys.ENTER)
        # pyautogui.press('enter')
        list=Select(self.dr.deduct())                #是否扣分
        count = len(list.options)
        index = random.randint(0, count - 1)
        list.select_by_index(index)
        self.dr.keepout_view().click()
        self.dr.leave_cause().send_keys(leave_cause) #输入请假原因
        self.dr.save_button1().click()               #点击保存（保存不成功）



    # @ddt.file_data(TestData_dir+"ClassManagement_wxj.yaml")
    def stu_leave_select(self,region,status_leave,leave_stu_name):   #学员请假信息查询
        self.dr.cls_management().click()                    #点击班务管理
        self.dr.stu_leave().click()                         #点击学员请假
        time.sleep(3)
        time.sleep(3)
        self.dr.stu_leave().click()                         #点击学员请假
        time.sleep(2)

        list=Select(self.dr.region())                       #区域
        list.select_by_visible_text(region)
        time.sleep(2)
        list=Select(self.dr.status_leave())                 #请假状态
        list.select_by_visible_text(status_leave)
        time.sleep(2)
        self.dr.leave_stu_name().send_keys(leave_stu_name)  #输入请假学生姓名
        self.dr.select_button().click()                     #点击查询按钮
        time.sleep(2)
        tableObj=self.dr.stu_leave_table()                  #获取学生请假的表单对象
        return readTable(tableObj)                           # 调用读表函数，得到二维列表

    # @ddt.file_data(TestData_dir + "ClassManagement_wxj.yaml")
    def stu_change(self,school_region,change_cls):           # 学员转班（已实现随机修改）
        self.dr.cls_management().click()                    #点击班务管理

        time.sleep(2)
        self.dr.stu_change().click()                        #点击学员转班
        time.sleep(2)
        self.dr.leave_cls().click()                         #点击转班按钮
        list=Select(self.dr.school_region())                #选择校区
        list.select_by_visible_text(school_region)
        list=Select(self.dr.change_cls())                   #选择班级
        list.select_by_visible_text(change_cls)
        self.dr.accept_button().click()                     #点击确定
        time.sleep(2)
        self.dr.accept().click()                            #点击确定
        time.sleep(2)

        return (self.dr.change_cls_success().text)          #返回转班成功的文本信息

        self.dr.accept1().click()
        # return (self.obj.change_cls_success().text)          #返回转班成功的文本信息
        return (self.dr.souso().text)          #返回转班成功的文本信息


    def stu_change_query(self,school_region,change_cls):              #学员转班信息查询
        self.dr.cls_management().click()                    #点击班务管理
        self.dr.stu_change().click()                        #点击学员转班
        time.sleep(2)
        list=Select(self.dr.region())                       #区域
        list.select_by_visible_text(school_region)
        list=Select(self.dr.cls_room())                     #班级
        list.select_by_visible_text(change_cls)
        self.dr.search_button().click()                     #点击搜索
        tableObj=self.dr.tableObj()                         #或去表单对象
        return readTable(tableObj)

    def add_company(self,company,industry,address,name,phone):
        self.dr.click_jiuye().click()    #点击就业管理
        self.dr.click_client().click()     #点击企业客户
        self.dr.click_add_company().click()     #点击新增企业
        self.dr.get_company_name().send_keys(company)
        self.dr.get_industry().send_keys(industry)
        self.dr.get_address().send_keys(address)
        self.dr.get_contacts().send_keys(name)
        self.dr.get_phone().send_keys(phone)
        self.dr.click_add().click()

    def search_company(self,city,class_name,direction):             #搜索入职信息
        self.dr.click_jiuye().click()       #点击就业管理
        self.dr.click_entry_information().click()      #点击入职信息
        selectlist=Select(self.dr.click_select_range())     #点击区域的下拉框
        selectlist.select_by_visible_text(city)      #选择成都
        selectlist = Select(self.dr.click_select_class())    #点击班级的下拉框
        selectlist.select_by_visible_text(class_name)    #选择29期的班级
        selectlist=Select(self.dr.click_select_direction())    #点击测试方向的下拉框
        selectlist.select_by_visible_text(direction)      #选择测试
        self.dr.click_search_entry_information().click()

    def add_join_company(self,city,class_name,direction,person_positon,salary,company_name):
        self.dr.click_jiuye().click()  # 点击就业管理
        self.dr.click_entry_information().click()  # 点击入职信息
        selectlist = Select(self.dr.click_select_range())  # 点击区域的下拉框
        selectlist.select_by_visible_text(city)  # 选择成都
        selectlist = Select(self.dr.click_select_class())  # 点击班级的下拉框
        selectlist.select_by_visible_text(class_name)  # 选择29期的班级
        selectlist = Select(self.dr.click_select_direction())  # 点击测试方向的下拉框
        selectlist.select_by_visible_text(direction)  # 选择测试
        self.dr.click_add_join().click()      #点击新增的按钮
        selectlist=Select(self.dr.get_joinposition())   #点击入职岗位的下拉框
        selectlist.select_by_visible_text(person_positon)    #选择测试工程师
        time.sleep(2)
        self.dr.get_joindata().click()      #点击选择时间的下拉框
        self.dr.get_detail_time().click()   #点击选择的时间
        self.dr.get_salary().send_keys(salary)    #薪水多少钱
        self.dr.get_select_company().click() #点击选择公司的下拉框
        self.dr.get_input().send_keys(company_name)     #点击输入企业名称
        self.dr.click_input().click()         #点击企业名称确认
        self.dr.click_save().click()   #保存

    def add_apply(self,flag):
        if flag == 1:
            self.dr.get_teaching_management_ele().click()  # 点击教学管理

        time.sleep(2)
        self.dr.get_overtime_apply_ele().click()               # 点击加班申请
        table1 = readTable(self.dr.get_apply_table_ele())
        id = table1[0][0]
        # print(type(id))     str
        self.dr.get_add_button_ele().click()                   # 点击新增按钮

        self.dr.get_apply_start_time().click()                 # 点击加班申请开始时间
        self.dr.get_start_day_ele().click()                    # 点击日期
        time.sleep(2)
        self.dr.get_start_hour_ele().click()                   # 点击小时
        time.sleep(2)
        self.dr.get_start_minute_ele().click()                 # 点击分钟

        self.dr.get_apply_end_time().click()                   # 点击加班申请结束时间
        self.dr.get_end_day_ele().click()
        time.sleep(2)
        self.dr.get_end_hour_ele().click()
        time.sleep(2)
        self.dr.get_end_minute_ele().click()


        selectList = Select(self.dr.get_apply_accounting_way_ele())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)           # 下拉框选择申请调休


        selectList = Select(self.dr.get_belong_region())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)
        selectText = selectList.first_selected_option.text


        self.dr.get_apply_reason_ele().send_keys('aaaaaaaaaa')    # 输入申请原因

        self.dr.get_submit_button_ele().click()                   # 点击提交


        self.dr.get_confirm_button().click()                      # 点击确认
        # ActionChains(self.tm.driver).click(self.tm.get_confirm_button()).perform()
        # print(readTable(self.tm.get_apply_table_ele()))
        table2 = readTable(self.dr.get_apply_table_ele())
        # print(table2[0][0])
        # print(type(table2[0][0]))      str
        return id,table2,selectText

    def apply_reason(self):
        # self.tm.get_teaching_management_ele().click()  # 点击教学管理
        self.dr.get_overtime_apply_ele().click()  # 点击加班申请
        self.dr.get_add_button_ele().click()  # 点击新增按钮

        self.dr.get_apply_start_time().click()  # 点击加班申请开始时间
        self.dr.get_start_day_ele().click()  # 点击日期
        time.sleep(2)
        self.dr.get_start_hour_ele().click()  # 点击小时
        time.sleep(2)
        self.dr.get_start_minute_ele().click()  # 点击分钟

        self.dr.get_apply_end_time().click()  # 点击加班申请结束时间
        self.dr.get_end_day_ele().click()
        time.sleep(2)
        self.dr.get_end_hour_ele().click()
        time.sleep(2)
        self.dr.get_end_minute_ele().click()

        selectList = Select(self.dr.get_apply_accounting_way_ele())
        count = len(selectList.options)
        index = random.randint(1,count-1)
        selectList.select_by_index(index)
        accountText = selectList.first_selected_option.text

        self.dr.get_apply_reason_ele().send_keys('aaaaaaaaa')  # 输入申请原因

        self.dr.get_submit_button_ele().click()                   # 点击提交

        # print(self.tm.get_apply_reason_clue())
        time.sleep(2)        # 必须加延时，不然报错

        try:
            apply_reason_clue = self.dr.get_apply_reason_clue().text
            self.dr.get_exit_reason_clue().click()
            time.sleep(2)  # 必加
            self.dr.get_exit_apply().click()
            return apply_reason_clue
        except Exception as e:
            return '测试失败'





    def assign_work(self):
        # self.tm.get_teaching_management_ele().click()  # 点击教学管理
        self.dr.get_on_duty_ele().click()  # 点击教师值班
        self.dr.get_assign_work_ele().click()  # 点击指定值班
        time.sleep(2)
        selectList = Select(self.dr.get_workman_select_ele())
        count = len(selectList.options)  # 下拉框的长度
        index = random.randint(1, count - 1)   # 下标为0是全部，必须从1开始
        selectList.select_by_index(index)
        # print(self.tm.get_workman_select_ele().text)
        selectedText = selectList.first_selected_option.text

        # select_province_type = Select(self.driver.find_element_by_xpath('//*[@name="top"]'))  # 定位下拉框
        # print(select_province_type.first_selected_option.text)  # 获取下拉框当前选中得值
        # select_province_type.select_by_value('B')  # 选择下拉框得另一个选项
        # time.sleep(1)
        # print(select_province_type.first_selected_option.text)  # 重新获取下拉框选择的值



        self.dr.get_workdata_ele().click()  # 点击值班日期输入框
        # time.sleep(3)
        self.dr.get_date_ele().click()  # 点击日期
        # time.sleep(3)
        self.dr.get_dutyinfo_save_ele().click()  # 点击保存值班信息按钮
        self.dr.get_dutyinfo_confirm_ele().click()  # 点击确认按钮
        # print(readTable(self.tm.get_dutyinfo_table_ele()))
        return readTable(self.dr.get_dutyinfo_table_ele()),selectedText



    def sys_employee_query(self):
        self.dr.get_sys_management_ele().click()
        self.dr.get_user_management().click()
        self.dr.get_employee_name_ele().send_keys('尚天')
        self.dr.get_query_button_ele().click()
        return readTable(self.dr.get_sys_emloyee_query_table_ele())